package com.rocketmq.example;

import java.util.ArrayList;
import java.util.List;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

public class E07_SyncMessage {

    private static final String TOPIC = "SectionOrderMessage";

    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer(TOPIC);
        producer.setNamesrvAddr(ExampleMQConfig.ENDPOINT.getName());

        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        try {
            List<Order> orderList = new E07_SyncMessage().buildOrders();
            for (int i = 0; i < orderList.size(); i++) {
                Message message = new Message(TOPIC, ExampleMQConfig.TAG.getName(), orderList.get(i).toString().getBytes());
                SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        return mqs.get((int) ((long) arg % mqs.size()));
                    }

                }, orderList.get(i).getOrderId());
                System.err.println(sendResult);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }

    private static class Order {
        private long orderId;
        private String desc;

        public long getOrderId() {
            return orderId;
        }

        public void setOrderId(long orderId) {
            this.orderId = orderId;
        }

        @SuppressWarnings("unused")
        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        @Override
        public String toString() {
            return "Order [orderId=" + orderId + ", desc=" + desc + "]";
        }

    }

    private List<Order> buildOrders() {
        List<Order> orderList = new ArrayList<>();
        Order order = new Order();
        // 订单1
        order.setOrderId(001);
        order.setDesc("创建");
        orderList.add(order);

        order = new Order();
        order.setOrderId(001);
        order.setDesc("付款");
        orderList.add(order);

        order = new Order();
        order.setOrderId(001);
        order.setDesc("推送");
        orderList.add(order);

        order = new Order();
        order.setOrderId(001);
        order.setDesc("完成");
        orderList.add(order);

        // 订单2
        order = new Order();
        order.setOrderId(002);
        order.setDesc("创建");
        orderList.add(order);

        order = new Order();
        order.setOrderId(002);
        order.setDesc("付款");
        orderList.add(order);

        order = new Order();
        order.setOrderId(002);
        order.setDesc("推送");
        orderList.add(order);

        order = new Order();
        order.setOrderId(002);
        order.setDesc("完成");
        orderList.add(order);

        // 订单3
        order = new Order();
        order.setOrderId(003);
        order.setDesc("创建");
        orderList.add(order);

        order = new Order();
        order.setOrderId(003);
        order.setDesc("付款");
        orderList.add(order);

        order = new Order();
        order.setOrderId(003);
        order.setDesc("推送");
        orderList.add(order);

        order = new Order();
        order.setOrderId(003);
        order.setDesc("完成");
        orderList.add(order);

        return orderList;
    }
}
